<?php

namespace App\Repository\TaggingConfig;

use App\Models\TaggingConfig as model;
use App\Repository\BaseRepository;

//主播
class TaggingConfigRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new model();
//    $this->log = new Logger('anchor');
//    $this->log->pushHandler(
//      (new RotatingFileHandler(storage_path('/logs/anchor.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//    );
        parent::__construct();
    }

    public function index($params)
    {
        $rows = $this->model->get();
        return $this->returnMsg(200, '查询完毕', $rows);
    }

    public function add($params)
    {
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'kk' => 'key值不能为空',
            'name' => '名字不能为空',
            'color' => '颜色不能为空'
        ];
        $insertData = $this->getRealData($params, array_keys($msgField), $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        $isTrue = $this->model->where(['name' => $insertData['name']])->first();
        if ($isTrue) {
            return $this->returnMsg(1, '名字已经存在');
        }
        $isTrue = $this->model->where(['kk' => $insertData['kk']])->first();
        if ($isTrue) {
            return $this->returnMsg(1, 'key值已经存在');
        }
        $insertData['shop_id'] = $params['user']['shop_id'];
        $insertData['created_at'] = date("Y-m-d H:i:s");
        $insertData['updated_at'] = date("Y-m-d H:i:s");
        $res = $this->model->insert($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功');
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($id)
    {
        $where = ["id" => $id];
        $result = $this->model->where($where)->count();
        if ($result) {
            $del = $this->model->where($where)->delete();
            return $this->returnMsg(200, "删除成功");
        }
        return $this->returnMsg(1, "数据不存在");
    }

    //修改
    public function edit($params)
    {

        $optionalFields = $this->model->getTableField();
        $msgField = [
            'id' => '指定id不能为空',
            'kk' => 'key值不能为空',
            'name' => '名字不能为空',
            'color' => '颜色不能为空'
        ];
        $upData = $this->getRealData($params, array_keys($msgField), $optionalFields, $msgField);
        if (!empty($upData) && is_string($upData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $upData
            );
        }
        $id = $params['id'];
        $isTrue = $this->model->where([['name', '=', $upData['name']], ['id', '<>', $id]])->first();
        if ($isTrue) {
            return $this->returnMsg(1, '名字已经存在');
        }
        $isTrue = $this->model->where([['kk', '=', $upData['kk']], ['id', '<>', $id]])->first();
        if ($isTrue) {
            return $this->returnMsg(1, 'key值已经存在');
        }
        $where = [['id', '=', $id]];
        $haveData = $this->model->where($where)->count("id");
        if (!$haveData) {
            return $this->returnMsg(100, '未查询到修改的数据');
        }
        $upData['updated_at'] = date("Y-m-d H:i:s");
        $res = $this->model->where($where)->update($upData);
        if (false !== $res) {
            return $this->returnMsg(200, '修改成功!');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    public function getTagging()
    {
        $rows = [];
        foreach ($this->model->select(['kk', 'name', 'color', 'remark'])->orderBy('kk', 'asc')->cursor() as $row) {
            $row['id'] = $row['kk'];
            $rows[$row['kk']] = $row;
        }
        return $rows;
    }
}
